package com.sec.etech.sys.util;

import com.sec.etech.org.service.EtechUserUtil;
import com.sec.etech.sys.service.EtechDictServiceImpl;
import org.openbpm.base.core.util.AppUtil;
import org.openbpm.org.api.model.IGroup;
import org.openbpm.sys.api.model.dto.DataDictDTO;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

public class ContextUtil extends org.openbpm.sys.util.ContextUtil {

    public static String getWebUrlPrefix(){
        return PropertyUtil.getPropertyService().getByAlias("webUrlPrefix","http://etech.stcetech.ad01.sec.com");
    }

    public static String getBackUrlPrefix(){
        return PropertyUtil.getPropertyService().getByAlias("backUrlPrefix","http://etech.stcetech.ad01.sec.com/api");
    }

    public static String[] getCurrentGroupCodes() {
        Set<String> codes = new HashSet();
        List<IGroup> groupList = getCurrentConext().getCurrentGroups();
        if (groupList != null && !groupList.isEmpty()) {
            Iterator var2 = groupList.iterator();

            while(var2.hasNext()) {
                IGroup group = (IGroup)var2.next();
                codes.add(group.getGroupCode());
            }
        }

        return codes.toArray(new String[codes.size()]);
    }

    // 当前部门以及子部门
    public static String[] getCurrentGroupIds() {
        Set<String> ids = new HashSet();
        List<IGroup> groupList = getCurrentConext().getCurrentGroups();
        if (groupList != null && !groupList.isEmpty()) {
            Iterator var2 = groupList.iterator();

            while(var2.hasNext()) {
                IGroup group = (IGroup)var2.next();
                ids.add(group.getGroupId());
            }
        }
        return ids.toArray(new String[ids.size()]);
    }


    public static String[] getCurrentStatIds() {
        String currentUserId = getCurrentUserId();
        return EtechUserUtil.getEtechGroupService().getCurrentStatIds(currentUserId);
    }

    // 首页待办显示
    public static String[] getCurrentAttendenceIds() {
        String currentUserId = getCurrentUserId();
        return EtechUserUtil.getEtechGroupService().getCurrentAttendenceIds(currentUserId);
    }

    // 作首页待办显示+内勤
    public static String[] getCurrentMasterGroupIds() {
        String currentUserId = getCurrentUserId();
        return EtechUserUtil.getEtechGroupService().getCurrentMasterGroupIds(currentUserId);
    }

    // 首页待办显示
    public static String[] getCurrentMasterGroupIds2() {
        String currentUserId = getCurrentUserId();
        return EtechUserUtil.getEtechGroupService().getCurrentMasterGroupIds2(currentUserId);
    }

    public static String getCurrentMasterGroupName() {
        String currentUserId = getCurrentUserId();
        return EtechUserUtil.getEtechGroupService().getFullGroupNameByUserId(currentUserId);
    }
    /**
     * 判断是否为空
     * @param obj
     * @return
     */
    public static Boolean isEmpty(Object obj) {
        if (obj == null) {
            return true;
        }

        if (obj.equals("") || obj.equals("null")) {
            return true;
        }

        if (obj instanceof String) {
            if (((String) obj).trim().length() == 0) {
                return true;
            }
        } else if (obj instanceof Collection) {
            if (((Collection) obj).size() == 0) {
                return true;
            }
        } else if (obj instanceof Map) {
            if (((Map) obj).size() == 0) {
                return true;
            }
        }

        return false;
    }

    /**
     * 驼峰转下划线
     */
    public static String humpToLine(String str) {
        Pattern humpPattern = Pattern.compile("[A-Z]");
        Matcher matcher = humpPattern.matcher(str);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, "_" + matcher.group(0).toLowerCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }
    /**
     * 文本连接，能自动去掉空字符串和null值
     * @param txtList 需连接的文本
     * @param symbol 分隔字符
     * @return String
     * @author PengMaochang
     * @date 2021/9/1 10:10
     */
    public static String concat(List<String> txtList, String symbol) {
        if (isEmpty(txtList)) {
            return "";
        }
        StringBuilder concatString = new StringBuilder();
        for (int i = 0; i < txtList.size(); i++) {
            if (!isEmpty(txtList.get(i))) {
                concatString.append(txtList.get(i));
                if (i != txtList.size() - 1 && !isEmpty(symbol) && !isEmpty(txtList.get(i + 1))) {
                    concatString.append(symbol);
                }
            }
        }
        return concatString.toString();
    }

    /**
     * 获取某数据字典中key_对应的name_。没有时返回null，查到多个时返回第一个
     * @param dictKey 数据字典的key，例如user_ext
     * @param dataKey key_，例如用户id 1128
     * @return String name_，例如用户名 彭茂昌。没有时返回null，查到多个时返回第一个
     * @author PengMaochang
     * @date 2022/2/7 16:40
     */
    public static String getDictDataNameByKey(String dictKey,String dataKey){
        EtechDictServiceImpl etechDictService=(EtechDictServiceImpl)AppUtil.getBean("etechDictService");
        List<DataDictDTO> dictDTOList = etechDictService.getDictNodeList4grid(dictKey).stream().filter(
                item -> item.getKey().equals(dataKey)
        ).collect(Collectors.toList());
        if(dictDTOList!=null&&dictDTOList.size()!=0){
            return dictDTOList.get(0).getName();
        }else {
            return null;
        }
    }
}
